package com.example.devicemanage.controller;

import com.example.devicemanage.entity.Admin;
import com.example.devicemanage.entity.Equip;
import com.example.devicemanage.service.inter.EquipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class EquipController {
    @Autowired
    EquipService equipService;

    @RequestMapping(path = "/queryEquip",method = RequestMethod.GET)
        //获取请求参数方式3,直接使用原生的HttpServletRequest
    String queryAllequip(HttpServletRequest httpServletRequest) {
        int currentPage=1;
        int count=6;
        int totalPage=0;
        String targetPage=httpServletRequest.getParameter("targetPage");
        if(targetPage!=null){
            currentPage=Integer.parseInt(targetPage);
        }
        List<Equip> list1=new ArrayList<>();
        list1=equipService.queryAllequip();
        List<Equip> list=new ArrayList<>();
       if(list1.size()%6==0){
           totalPage=list1.size()/6;
       }else{
           totalPage=list1.size()/6+1;
       }
        if(currentPage*count<=list1.size()){
            for(int i=0;i<6;i++){
                list.add(list1.get((currentPage-1)*count+i));

            }
        }else{
            int m=list1.size()-(currentPage-1)*count;
            for(int j=0;j<m;j++){
                list.add(list1.get(list1.size()-m+j));
            }
        }


    httpServletRequest.setAttribute("list",list);
    httpServletRequest.setAttribute("totalPage",totalPage);
    httpServletRequest.setAttribute("equipNum",list1.size());
    httpServletRequest.setAttribute("currentPage",currentPage);

    return "equip-list";
    }

//把报修表单发送过去
    @RequestMapping(path = "/equip_repair", method = RequestMethod.GET)
    String doEquip_repair(HttpServletRequest httpServletRequest){
        String eid=httpServletRequest.getParameter("eid");
        httpServletRequest.setAttribute("eid",eid);
        return "equip_repair";
    }

    //把报废表单发送过去
    @RequestMapping(path = "/equip_break", method = RequestMethod.GET)
    String doEquip_break(HttpServletRequest httpServletRequest){
        String eid=httpServletRequest.getParameter("eid");
        httpServletRequest.setAttribute("eid",eid);
        System.out.println(eid);
        return "equip_break";
    }
    //通过搜索栏查询设备详细信息
    @RequestMapping(path = "/querySingleequip",method = RequestMethod.GET)
        //获取请求参数方式3,直接使用原生的HttpServletRequest
    String querySingleequip(HttpServletRequest httpServletRequest) {
        String ename=httpServletRequest.getParameter("ename");
        Equip equip=equipService.queryEquip(ename);

        httpServletRequest.setAttribute("e",equip);

        return "specific-info";
    }

    //删除指定的设备
    @RequestMapping(path = "/deleteEquip",method = RequestMethod.GET)
    @ResponseBody
    //获取请求参数方式3,直接使用原生的HttpServletRequest
    int deleteEquip(HttpServletRequest httpServletRequest) {
        String eid=httpServletRequest.getParameter("eid");

         if(equipService.deleteEquip_name(eid)){
          return 1;
         }
       else{
             return 0;
         }

    }

    //展示被修改设备信息
    @RequestMapping(path = "/queryEequipByid",method = RequestMethod.GET)
    //获取请求参数方式3,直接使用原生的HttpServletRequest
    String queryEequipByname(HttpServletRequest httpServletRequest) {
        String eid=httpServletRequest.getParameter("eid");
        String currentPage=httpServletRequest.getParameter("targetPage");
        System.out.println(currentPage);
        Equip equip=equipService.querySingEquip(eid);
        httpServletRequest.setAttribute("e",equip);
        httpServletRequest.setAttribute("currentPage",currentPage);
        return "equip_update";
    }

    //更新设备信息
    @RequestMapping(path = "/updateEquipinfo",method = RequestMethod.GET)
    //获取请求参数方式3,直接使用原生的HttpServletRequest
    String updateEquipinfo(HttpServletRequest httpServletRequest) {
      String eid=httpServletRequest.getParameter("eid");
      String ename=httpServletRequest.getParameter("ename");
      String category=httpServletRequest.getParameter("category");
      String model=httpServletRequest.getParameter("model");
      String spec=httpServletRequest.getParameter("spec");
      String pdate=httpServletRequest.getParameter("pdate");
      String agent=httpServletRequest.getParameter("agent");
      String manufacturer=httpServletRequest.getParameter("manufacturer");
      String num=httpServletRequest.getParameter("num");
      Equip e=new Equip();
      e.setEname(ename);
      e.setCategory(category);
      e.setModel(model);
      e.setSpec(spec);
      e.setPdate(pdate);
      e.setAgent(agent);
      e.setManufacturer(manufacturer);
      e.setPrice(12);
       equipService.updateEquipinfo(e,eid);

        return queryAllequip(httpServletRequest);
    }




    @RequestMapping(path = "/saveEequipinfo",method = RequestMethod.POST)
    //获取请求参数方式3,直接使用原生的HttpServletRequest
    String saveEequipinfo(HttpServletRequest httpServletRequest) {
        String eid=httpServletRequest.getParameter("eid");
        String ename=httpServletRequest.getParameter("ename");
        String price=httpServletRequest.getParameter("price");
        String category=httpServletRequest.getParameter("category");
        String model=httpServletRequest.getParameter("model");
        String spec=httpServletRequest.getParameter("spec");
        String num=httpServletRequest.getParameter("num");
        String pdate=httpServletRequest.getParameter("pdate");
        String slife=httpServletRequest.getParameter("slife");
        String manufacturer=httpServletRequest.getParameter("manufacturer");
        String state=httpServletRequest.getParameter("state");
        String agent=httpServletRequest.getParameter("agent");

        //生成Equip对象
        Equip equip1=new Equip();

        return "equip_info";
    }

    //报修时，修改设备状态为1
    @RequestMapping(path = "/updateEquipstate",method = RequestMethod.POST)
    //获取请求参数方式3,直接使用原生的HttpServletRequest
    void updateEquipstate(String eid) {
       equipService.updateState(eid);

    }
   //报废时，修改设别状态为2

   @RequestMapping(path = "/updateEquipstateThree",method = RequestMethod.POST)
   //获取请求参数方式3,直接使用原生的HttpServletRequest
   void updateEquipstateThree(String eid) {
       equipService.updateStateThree(eid);

   }

    //设备修好，申请上线，状态修改为0
    @RequestMapping(path = "/updateEquipstateTwo",method = RequestMethod.GET)
    //获取请求参数方式3,直接使用原生的HttpServletRequest
    String updateEquipstateTwo(HttpServletRequest httpServletRequest) {
        String eid=httpServletRequest.getParameter("eid");
        equipService.updateStateTwo(eid);
       return queryAllequip(httpServletRequest);

    }





}